package cmd

import (
	"fmt"

	"sale-admin/config/mysql"
	"sale-admin/internal/migrate"

	"github.com/spf13/cobra"
)

var forceMigrate bool

func init() {
	rootCmd.PersistentFlags().BoolVarP(&forceMigrate, "force", "f", false, "force migrate")
	rootCmd.AddCommand(migrateCmd)
}

var migrateCmd = &cobra.Command{
	Use:   "migrate",
	Short: "migrate",
	Run: func(*cobra.Command, []string) {
		db := mysql.NewDB()
		hasDB := db.Migrator().HasTable("version")
		// 首次自动安装
		if !hasDB {
			if err := migrate.Install(); err != nil {
				fmt.Println(err.Error())
				panic(err.Error())
			}
		} else {
			// 数据库迁移
			migrate.Migrate()
		}
	},
}
